package com.itic.mall.mobile.uc;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.mall.mobile.base.BaseMobileInterface;
import com.itic.mall.system.marketuser.persistence.model.MarketUser;
import com.itic.mall.system.marketuserproduct.persistence.model.MarketUserProduct;
import com.itic.mall.system.marketuserproduct.persistence.model.MarketUserProductExample;
import com.itic.mall.system.marketuserproduct.persistence.model.MarketUserProductExample.Criteria;
import com.itic.mall.system.marketuserproduct.service.MarketUserProductService;
import com.itic.mall.system.user.persistence.model.MallUser;


/**
 * 用户个人中心接口类
 * @author 许小宝
 * 2017年07月25日  14时11分
 */
@Controller
@RequestMapping(value="/mobile/uc")
public class MallUcInterface
     extends BaseMobileInterface {
   
	@Autowired
	protected MarketUserProductService marketUserProductService;
	
    /**
     * APP用户登入个人中心首页
     * @return
     */
    @RequestMapping(value="/index")
    @ResponseBody
    public Map<String,Object> index(HttpServletRequest request,HttpServletResponse response,String token){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String result = FAIL;
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		//判断必要的参数
    		if( StringHelper.isNotBlank(token) ){
    			MallUser user = getMallUserByToken(token);
    			if( null != user ){
    				Map<String, Object> dataMap = new HashMap<String,Object>();
    				
    				if(StringHelper.isBlank(user.getUserHeadImg())){
    					user.setUserHeadImg("http://file.jhjvip.cn/images/default.jpg");
    				}
    				
	            	dataMap.put("money", user.getMoney());//用户余额
	            	
	            	dataMap.put("headImg", user.getUserHeadImg());//用户头像地址
	            	dataMap.put("nickName", user.getUserNickName());//用户昵称
	            	dataMap.put("phone", user.getUserPhone());//用户是否绑定手机号码
	            	resultMap.put(DATA, dataMap);
    				result = SUCCESS;
        			errorCode = SUCCESS_ERROR_CODE;
                	errorMsg = SUCCESS_ERROR_MSG;  	
    			}else{
    				result = LOGIN;
        			errorCode = LOGIN_ERROR_CODE;
                	errorMsg = LOGIN_ERROR_MSG;  	
    			}
    		}
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
        	//log(e);
        	logException("MallUcIndexInterface", "index", e.toString(),"个人中心首页",e);
    	}
    	resultMap.put(RESULT, result);
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    } 
    
    
    /**
     * APP用户登入个人中心首页
     * @return
     */
    @RequestMapping(value="/msg")
    @ResponseBody
    public Map<String,Object> msg(HttpServletRequest request,HttpServletResponse response,String token){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String result = FAIL;
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		//判断必要的参数
    		if( StringHelper.isNotBlank(token) ){
    			MallUser user = getMallUserByToken(token);
    			if( null != user ){
	            	resultMap.put(DATA, user.getId());
    				result = SUCCESS;
        			errorCode = SUCCESS_ERROR_CODE;
                	errorMsg = SUCCESS_ERROR_MSG;  	
    			}else{
    				result = LOGIN;
        			errorCode = LOGIN_ERROR_CODE;
                	errorMsg = LOGIN_ERROR_MSG;  	
    			}
    		}
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
        	//log(e);
        	logException("MallUcIndexInterface", "index", e.toString(),"个人中心首页",e);
    	}
    	resultMap.put(RESULT, result);
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    } 
    
    
    /**
     * 用户修改昵称
     * @return
     */
    @RequestMapping(value="/edit")
    @ResponseBody
    public Map<String,Object> edit(HttpServletRequest request,HttpServletResponse response,String token,String name){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String result = FAIL;
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		//判断必要的参数
    		if( StringHelper.isNotBlank(token) && StringHelper.isNotBlank(name) ){
    			MallUser user = getMallUserByToken(token);
    			if( null != user ){
    				user.setUserNickName(name);
    				result = mallUserService.edit(user);
        			errorCode = SUCCESS_ERROR_CODE;
                	errorMsg = SUCCESS_ERROR_MSG;  	
    			}else{
    				result = LOGIN;
        			errorCode = LOGIN_ERROR_CODE;
                	errorMsg = LOGIN_ERROR_MSG;  	
    			}
    		}
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
        	//log(e);
        	logException("MallUcIndexInterface", "index", e.toString(),"个人中心首页",e);
    	}
    	resultMap.put(RESULT, result);
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    } 
    
    
    /**
     * APP用户登入个人中心首页
     * @return
     */
    @RequestMapping(value="/store")
    @ResponseBody
    public Map<String,Object> store(HttpServletRequest request,HttpServletResponse response,String userId,String categoryId, String orderType, String minPrice, String maxPrice, String minSale, String maxSale){
    	Map<String, Object> resultMap = new HashMap<String,Object>();
    	String result = FAIL;
    	String errorCode = PARAM_ERROR_CODE;
    	String errorMsg = PARAM_ERROR_MSG;
    	try{
    		//判断必要的参数
    		if( StringHelper.isNotBlank(userId) ){
    			MarketUser marketUser = marketUserService.get(userId);
    			if( null != marketUser ){
    				//添加店铺信息
    				Map<String, Object> dataMap = new HashMap<String,Object>();
	            	dataMap.put("store", marketUser);
	            	//添加商品信息
	            	
	            	MarketUserProductExample example = new MarketUserProductExample();
	            	
    				example = this.handlePageQuery(request, example);
    				
    				if( StringHelper.isNotBlank(orderType) ){
    					if( "priceDesc".equals(orderType) ){
    	    				example.setOrderByClause("PRODUCT_PRICE DESC");
    	    			}
    	    			if( "priceAsc".equals(orderType) ){
    	    				example.setOrderByClause("PRODUCT_PRICE ASC");
    	    			}
    	    			if( "salesDesc".equals(orderType) ){
    	    				example.setOrderByClause("TOTAL_SALES DESC");
    	    			}
    				}else{
    					example.setOrderByClause("CREATE_TIME DESC");
    				}
    				//Criteria criteria = example.createCriteria();
    				Criteria criteria = example.createCriteria();
    				criteria.andIsEffectiveEqualTo(DBConst.TRUE);
    				criteria.andUserIdEqualTo(marketUser.getId());
    				
    				if( StringHelper.isNotBlank(categoryId) && !"0".equals(categoryId) ){
    					criteria.andCategoryIdEqualTo(categoryId);
    				}
    				
    				if( StringHelper.isNoneBlank(minPrice) ){
    					criteria.andProductOrginPriceGreaterThanOrEqualTo(Double.parseDouble(minPrice));
    				}
    				
    				if( StringHelper.isNoneBlank(maxPrice) ){
    					criteria.andProductOrginPriceLessThanOrEqualTo(Double.parseDouble(maxPrice));
    				}
    				
    				
    				int count = marketUserProductService.count(example);
    				List<MarketUserProduct> list = marketUserProductService.list(example);
    				
    				dataMap.put("products", list);
    				dataMap.put("totalCount", count + "");
    				//判断必要的参数
    				resultMap.put(DATA, dataMap);
    				result = SUCCESS;
    				errorCode = SUCCESS_ERROR_CODE;
    	        	errorMsg = SUCCESS_ERROR_MSG; 
	            	
	            	
	            	resultMap.put(DATA, dataMap);
    				result = SUCCESS;
        			errorCode = SUCCESS_ERROR_CODE;
                	errorMsg = SUCCESS_ERROR_MSG;  	
    			}else{
        			errorCode = MARKET_EMPTY_ERROR_CODE;
                	errorMsg =  MARKET_EMPTY_ERROR_MSG;  	
    			}
    		}
    	}catch(Exception e){
    		errorCode = SERVER_ERROR_CODE;
        	errorMsg = SERVER_ERROR_MSG;
        	//log(e);
        	logException("MallUcIndexInterface", "index", e.toString(),"个人中心首页",e);
    	}
    	resultMap.put(RESULT, result);
    	resultMap.put(ERROR_CODE, errorCode);
    	resultMap.put(ERROR_MSG, errorMsg);
    	isCrrossDomain(response);
    	return resultMap;
    } 
}